﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

public class SoundManager : MonoBehaviour
{
    public static SoundManager Instance;
    public List<AudioEffect> audioEffects;
    public AudioEffect bgmAudioEffect;
    public float fadePercent;
    public float fadeDuration;

    private void Awake()
    {
        Instance = this;
    }

    public AudioEffect PlaySound(AudioClip clip, UnityAction<AudioEffect> callback = null, int order = 1,
        bool isFade = false,
        float volume = 0.8f,
        bool isLoop = false,
        bool is3D = false)
    {
        audioEffects ??= new List<AudioEffect>();

        AudioEffect audioEffect = null;
        foreach (var audio in audioEffects)
        {
            if (!audio.isUsing)
            {
                audioEffect = audio;
                break;
            }
        }

        if (audioEffect == null)
        {
            audioEffect = AudioEffect.Create();
            audioEffects.Add(audioEffect);
            audioEffect.transform.SetParent(transform);
        }

        audioEffect.Play(clip, callback, order, isFade, volume, isLoop, is3D);
        return audioEffect;
    }

    public AudioEffect PlayBgm(AudioClip clip, int order = 1, bool isFade = false, float volume = 0.6f,
        bool isLoop = true, bool is3D = false)
    {
        bgmAudioEffect = PlaySound(clip, null, order, isFade, volume, isLoop, is3D);
        return bgmAudioEffect;
    }
}